package com.google.code.util;

public class RAND {
	public static double Normal(double miu, double sigma2) {
		double N = 12;
		double x = 0, temp = N;
		do {
			x = 0;
			for (int i = 0; i < N; i++)
				x = x + (Math.random());
			x = (x - temp / 2) / (Math.sqrt(temp / 12));
			x = miu + x * Math.sqrt(sigma2);
		} while (x <= 0);

		double max = 1 / Math.sqrt(sigma2) / Math.sqrt(2 * Math.PI);

		double pd = 1 / Math.sqrt(sigma2) / Math.sqrt(2 * Math.PI)
				* Math.pow(Math.E, Math.pow(x - miu, 2) / -2 / sigma2);
		return pd / max;
	}

	public static double Poisson(double Lamda) {
		double x = 0, b = 1, c = Math.exp(-Lamda), u;
		do {
			u = Math.random();
			b *= u;
			if (b >= c)
				x++;
		} while (b >= c);
		return x;
	}
}
